import { Http } from "@/utils/request";
import { Toast } from "@/utils/toast";
import { delayTask } from "@/utils/timer";

const baseUrl = "/groupon2";

export const unifiedorder_api = (
  data: { order_id: string; pay_type: 1; pay_from: "miniprogram" },
  showError = true
) => {
  return Http.post<{
    unifiedorder: {
      errorCode: number;
      errorMsg: string;
      result: { prepay_info: any };
    };
  }>(`${baseUrl}/unifiedorder`, data)
    .then(resp => {
      return resp.unifiedorder.errorCode === 1
        ? {
            ...resp.unifiedorder.result.prepay_info,
            timeStamp: `${resp.unifiedorder.result.prepay_info.timestamp}`,
            nonceStr: resp.unifiedorder.result.prepay_info.noncestr,
            signType: "MD5",
          }
        : Promise.reject(new Error(resp.unifiedorder.errorMsg));
    })
    .catch((error: Error) => {
      showError && delayTask(60, () => Toast.show(error.message));
      return Promise.reject(error);
    });
};
